package com.kedou.app;

import com.kedou.domain.Fund;
import com.kedou.domain.ProjectItem;
import com.kedou.net.PostFollowProject;
import com.kedou.net.PostGoFund;
import com.kedou.util.DebugUtil;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 项目认筹 界面
 * 
 * @author zhouchao
 * 
 */
public class ProjectFundActivity extends BaseActivity implements
		OnClickListener {
	/**
	 * 左侧 返回按钮
	 */
	private Button mBtnBack;
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgSetting;
	/**
	 * 项目名称
	 */
	private TextView mTvProjectName;

	/**
	 * 项目团队
	 */
	private TextView mTvProjectTeam;
	/**
	 * 众筹金额
	 */
	private TextView mTvProjectMoney;
	/**
	 * 出让股权
	 */
	private TextView mTvProjectEquity;

	/**
	 * 融资阶段
	 */
	private TextView mTvProjectStage;
	/**
	 * 众筹份数
	 */
	private TextView mTvProjectCopies;
	/**
	 * 当前认筹比例
	 */
	private TextView mTvProjectCurrentPercent;
	/**
	 * 每份金额
	 */
	private TextView mTvProjectPerAmount;
	/**
	 * 所在地
	 */
	private TextView mTvLocation;
	/**
	 * 领投人
	 */
	private TextView mTvLedInvestorName;
	/**
	 * 投资人头像
	 */
	private ImageView mTvInvestorHead;

	/**
	 * 领投人认购比例
	 */
	private TextView mTvPercentLedInvestor;

	/**
	 * 项目图片
	 */
	private ImageView mImgProjectDesc;

	/**
	 * 投资多少占多少比例
	 */
	private TextView mTvInvestPercent;

	/**
	 * 确认认筹
	 */
	private Button mBtnConfirm;
	private Bundle mBundle;
	private ProjectItem mProjectItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBundle = getIntent().getBundleExtra("project");
		if (mBundle != null) {
			mProjectItem = mBundle.getParcelable("project_item");
		}
		setContentView(R.layout.activity_projectrenchou);
		initView();

		setListener();
		getFundFromServer();
	}

	/**
	 * 获取认筹信息
	 */
	private void getFundFromServer() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(ProjectFundActivity.this)) {
			new PostGoFund(PreferencesUtil.getString(ProjectFundActivity.this,
					"token"), mProjectItem.getProjectId(),
					PreferencesUtil.getString(ProjectFundActivity.this,
							"userId"), Config.SOURCE,
					Util.getImei(ProjectFundActivity.this),
					new PostGoFund.SuccessCallback() {

						@Override
						public void onSuccess(String msg, Fund fund) {
							mProgressDialog.dismiss();
							mTvProjectName.setText(fund.getProjectName());
							mTvProjectTeam.setText(fund.getTeam());
							mTvProjectMoney.setText(fund.getTotalMoney());
							mTvProjectEquity.setText(fund.getSellingStock());
							mTvProjectStage.setText(fund.getFinancingStage());
							mTvProjectCopies.setText(fund.getCopies());
							mTvProjectCurrentPercent.setText(fund
									.getCurrentAccounting());
							mTvProjectPerAmount.setText(fund.getEachMoney());
							mTvLocation.setText(fund.getLocation());
							mTvLedInvestorName.setText(fund.getName());
							mTvPercentLedInvestor.setText(fund.getAccounting());
							// TODO 领投人头像；
							// TODO 投资多少占多少比例 mTvInvestPercent还未计算
						}
					}, new PostGoFund.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(ProjectFundActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, ProjectFundActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setVisibility(View.VISIBLE);
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.title_renchou);
		mTvProjectName = (TextView) findViewById(R.id.tv_project_name);
		mTvProjectTeam = (TextView) findViewById(R.id.tv_project_team);
		mTvProjectMoney = (TextView) findViewById(R.id.tv_project_money);
		mTvProjectEquity = (TextView) findViewById(R.id.tv_project_equity);
		mTvProjectStage = (TextView) findViewById(R.id.tv_project_stage);
		mTvProjectCopies = (TextView) findViewById(R.id.tv_project_copies);
		mTvProjectCurrentPercent = (TextView) findViewById(R.id.tv_project_current_percent);
		mTvProjectPerAmount = (TextView) findViewById(R.id.tv_project_per_amount);
		mTvLocation = (TextView) findViewById(R.id.tv_project_location);
		mTvLedInvestorName = (TextView) findViewById(R.id.tv_project_led_investor);
		mTvInvestorHead = (ImageView) findViewById(R.id.img_investor_head);
		mTvPercentLedInvestor = (TextView) findViewById(R.id.tv_percent_led_investor);
		mImgProjectDesc = (ImageView) findViewById(R.id.img_project_desc);
		mTvInvestPercent = (TextView) findViewById(R.id.tv_invest_percent);
		mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mBtnBack.setOnClickListener(this);
		mBtnConfirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_confirm:
			Intent intent = new Intent(this, ConfirmFundActivity.class);
			intent.putExtra("project", mBundle);
			this.startActivity(intent);
			break;
		}
	}
}
